#include "GameMain.hpp"

namespace mindsweeper {
namespace main {

GameMain::GameMain() :
	window(sf::VideoMode(800, 600, 32), "Mindsweeper")
{
	// Create the GameManager
	this->gameManager = new GameManager(this);
}

GameMain::~GameMain() {
	delete(this->gameManager);
	// Zero out the pointer for safety.
	this->gameManager = nullptr;
}

int GameMain::exec() {
	while (this->window.IsOpened()) {
		// Retrieve and process events
		sf::Event event;
		while (this->window.GetEvent(event)) {
			// A new event is available. Process it
			this->processEvent(event);
		}

		// Update the timestamp, as a prereq for updating game state
		this->timeHandler.update();

		// Update game logic
		this->gameManager->update();

		// Render the game
		this->gameManager->render();

		this->window.Display();
	}
	// This doesn't do much right now.
	return 0;
}

TimeHandler& GameMain::getTimeHandler() {
	return this->timeHandler;
}

void GameMain::processEvent(sf::Event &event) {
	switch (event.Type) {
		case sf::Event::Closed:
			this->window.Close();
			break;
		default:
			// Forward the event to the GameManager
			this->gameManager->processEvent(event);
			break;
	}
}

// End namespaces
}
}
